import router from '@/router'

import pinia from '@/stores'
import { useUserInfoStore } from '@/stores/modules/userInfo'
import { ElMessage } from 'element-plus'
import PubSub from 'pubsub-js'
import { ref } from 'vue'
const userInfoStore = useUserInfoStore(pinia)


// 不用进行token检查的白名单路径数组
const whiteList = ['/login', '/index', '/discovery']

PubSub.subscribe('showLogin', (msg: any, data: any) => {
    // 路由加载前
    router.beforeEach((to, from, next) => {
        const token = userInfoStore.token;
        // 如果token存在(已经登陆或前面登陆过)
        if (token) {
            next()

        } else { // 没有token
            // 如果目标路径在白名单中(是不需要token的路径)
            if (whiteList.indexOf(to.path) !== -1) {
                // 放行
                next()
            } else {
                // 如果没在白名单中, 跳转到登陆路由携带原目标路径

                data()

            }
        }
    })


})
const token = userInfoStore.token;



// 路由加载后
router.afterEach(() => {

})
